<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/2
 * Time: 9:33
 */
namespace app\api\controller;
use think\Db;
class Boutique extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $useridddd = input('user_id');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($useridddd,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    //精品公开课
    public function index(){
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $id= input('param.id');
        $where['public_subject'] = $id;
        $where['is_hot'] = 1;
        $where['public_is_on'] = 1;
        
        // 用户年级条件
        $user_id = input('user_id');
        $user_info = Db::table('up_u_user')
            ->where('user_id', $user_id)
            ->field('user_grade')
            ->find();
        $small = [1,2,3,4,5,6];
        $middle = [7,8,9];
        $hight = [10,11,12];
        if(in_array($user_info['user_grade'], $small)){
            $where['public_grade'] = ['IN', $small];
        }
        if(in_array($user_info['user_grade'], $middle)){
            $where['public_grade'] = ['IN', $middle];
        }
        if(in_array($user_info['user_grade'], $hight)){
            $where['public_grade'] = ['IN', $hight];
        }
        
        $res = Db::table('up_public_class')->where($where)
            ->limit($num*$limit,$num)
            ->order('public_is_top asc, public_time desc')
            ->select();
        foreach ($res as $key=>&$vo){
            $count = Db::table('up_public_apply')->where('apply_public_id',$vo['public_id'])->count();
            $vo['num'] = $count;
        }
        return  json(array('status'=>200,'msg'=>$res));
    }

    //课程详情
    public function detail(){
        extract(input());
        $where['public_id'] = input('param.id');
        $res = Db::table('up_public_class')->where($where)
            ->find();
        //查询报名人数
        $count = Db::table('up_public_apply')->where('apply_public_id',$res['public_id'])->count();
        $res['num'] = $count;
        //查询老师信息
        $info = Db::table('up_u_user')->where('user_id',$res['public_teacher_id'])
                              ->field('user_status,user_id,user_username,user_gender,user_intro,user_grade,
                               user_status,user_image')
                              ->find();
        //星级
        $star = $this->teacherStar($info['user_id']);
        //年级
        $grade = Db::table('up_grade')->where('grade_id',$info['user_grade'])->value('grade_name');
        //是否购买课程
        $map['purchase_type'] =1;
        $map['f_id'] =input('param.id');
        $map['purchase_user_id'] =$user_id;
        $data = Db::table('up_purchase')->where($map)->find();
        if($data['purchase_id']!=''){
            $res['is_money'] = 1;//购买
        }else{
            $res['is_money']  =2;
        }

        $res['star'] = $star;
        $res['grade'] = $grade;
        $res['user_gender'] = $info['user_gender'];
        $res['user_intro'] = $info['user_intro'];
        $res['user_status'] = $info['user_status'];
        $res['user_image'] = $info['user_image'];
        $res['user_username'] = $info['user_username'];
        $res['user_status'] = $info['user_status'];
        Db::table('up_u_user')->where('user_id',$res['public_teacher_id'])->setInc('user_sort',$star*0.7);

        return  json(array('status'=>200,'msg'=>$res));
    }
    
    //课程目录
    public function catalog(){
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['section_public_id'] = input('param.id');
        $res = Db::table('up_public_section')
            ->where($where)
            ->limit($num*$limit,$num)
            ->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    //购买课程
    public function buyClass(){
        extract(input());
        $where['public_id'] = input('param.id');
        $res = Db::table('up_public_class')->where($where)
            ->find();
        //查询报名人数
        $count = Db::table('up_public_apply')->where('apply_public_id',$res['public_id'])->count();
        $res['num'] = $count;
        //查询这个用户的平台币
        $user_balance = Db::table('up_u_user')->where('user_id',$user_id)->value('user_balance');
        $res['user_balance'] = $user_balance;
        return  json(array('status'=>200,'msg'=>$res));
    }

    //确认支付
    public function affirm(){
        extract(input());
        $id = input('param.id');
        $money = input('param.money');
        $teacher_id = input('param.teacher_id');
        $map['apply_user_id'] = $user_id;
        $map['apply_public_id'] = $id;
        $info = Db::table('up_public_apply')->where($map)->find();
        if($info['apply_id']!=''){
            return  json(array('status'=>100,'msg'=>"已经购买过,不能重复购买"));
        }
        $info1 = Db::table('up_public_class')->where('public_id',$id)->find();
        if($info1['public_type']==2){
            $user_balance = Db::table('up_u_user')->where('user_id',$user_id)->value('user_balance');
            if($money>$user_balance){
                return  json(array('status'=>100,'msg'=>"平台币不足,请先充值"));
            }else{
                Db::table('up_u_user')->where('user_id',$user_id)->setDec('user_balance',$money);
            }
        }
        Db::startTrans();
        try{
            //插入用户购买课程表
            $data['apply_user_id'] = $user_id;
            $data['apply_public_id'] = $id;
            $data['apply_time'] = date('Y-m-d H:i:s',time());
            Db::table('up_public_apply')->insertGetId($data);
            //todo
            //插入订单表
            $data1['order_type'] = 4;
            $data1['order_price'] = $money;
            $data1['order_time'] = date('Y-m-d H:i:s',time());
            $data1['order_user_id'] = $user_id;
            Db::table('up_order')->insertGetId($data1);
            $data3['order_type'] = 4;
            $data3['order_price'] = $money;
            $data3['order_time'] = date('Y-m-d H:i:s',time());
            $data3['order_user_id'] = $teacher_id;
            Db::table('up_order')->insertGetId($data3);
            //插入购买记录表
            $info1 = Db::table('up_public_class')->where('public_id',$id)->find();
            $data2['teacher_id'] = $info1['public_teacher_id'];
            $data2['purchase_mony'] = $money;
            $data2['purchase_user_id'] = $user_id;
            $data2['purchase_car_type'] = 1;
            $data2['purchase_type'] = 1;
            $data2['f_id'] = $id;
            if($info1['public_from'] == 1){ // 老师
                $data2['purchase_target'] = 1;
            }else{ // 后台
                $data2['purchase_target'] = 2;
            }
            $data2['purchase_time'] = date('Y-m-d H:i:s',time());
            Db::table('up_purchase')->insertGetId($data2);
            
            if($info1['public_from'] == 1){
                // 增加老师余额
                Db::table('up_u_user')->where('user_id', $info1['public_teacher_id'])->setInc('user_balance', $money);
                // 消息表
                allInNotice('公开课报名', '有学生报名了您的公开课，快来看看~', 2, 2, 6, $id, $info1['public_teacher_id']);
            }
            Db::commit();
            $res = 1;
        }catch(\Exception $e){
            Db::rollback();
            $res = 2;
        }
        if($res == 1){
            if($info1['public_from'] == 1){
                // 极光推送
                $list = [];
                $val = (string)$info1['public_teacher_id'];
                array_push($list, $val);
                try{
                    $this->JpushT(1, $list, '公开课报名', ['public_id' => $id, 'send_type' => 3]);
                }catch(\Exception $e){
        
                }
            }
            return  json(array('status'=>200,'msg'=>"购买成功"));
        }else{
            return  json(array('status'=>0,'msg'=>"购买失败"));
        }
    }
}